//
//  HJStatusCell.swift
//  zhangmiSinweibo
//
//  Created by zhangmi on 16/5/27.
//  Copyright © 2016年 Paramount Pictures. All rights reserved.
//更新约束 改变约束的对象和自身位置的调整

import UIKit
import SnapKit

class HJStatusCell: UITableViewCell {

    //声明约束对象属性
    var toolbarTopCons: Constraint?

    var statusViewModel: HJStatusViewModel? {
        didSet {
            //给原创微博的模型赋值
            statusOriginalView.statusViewModel = statusViewModel
            statusToolBar.statusViewModel = statusViewModel
            
        //判断是否是原创微博
        //卸载工具条的顶部约束对象
        toolbarTopCons?.uninstall()
        if statusViewModel?.status?.retweeted_status == nil {
           //原创微博
           //设置数据
           statusRetweetedView.statusViewModel = nil
           //隐藏转发微博视图
           statusRetweetedView.hidden = true
           //更新约束 底部工具条的顶部应该相对于 原创微博底部
           statusToolBar.snp_updateConstraints(closure: { (make) in
            self.toolbarTopCons = make.top.equalTo(statusOriginalView.snp_bottom).constraint
            })
        } else {
            //转发微博
            //设置数据
            statusRetweetedView.statusViewModel = statusViewModel
            //隐藏转发微博视图
            statusRetweetedView.hidden = false
            //更新约束 底部工具条的顶部应该相对于 原创微博底部
            statusToolBar.snp_updateConstraints(closure: { (make) in
                
                self.toolbarTopCons = make.top.equalTo(statusRetweetedView.snp_bottom).constraint
             })
         }
     }
 }
    

    //从xib 加载会调用这个方法
    override func awakeFromNib() {
        super.awakeFromNib()
  
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.selectionStyle = .None
        //self.backgroundColor = randomColor()
        setupUI()

    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置界面
    private func setupUI() {
        //自定义cell添加视图不能够直接使用cell添加 需要使用cell的contentView添加子视图
        contentView.addSubview(statusOriginalView)
        contentView.addSubview(statusRetweetedView)
        contentView.addSubview(statusToolBar)

        //设置约束
        statusOriginalView.snp_makeConstraints { (make) in
            //TODO
            make.top.right.left.equalTo(self.contentView)

            //设置一个测试的高度
            //make.height.equalTo(100)
        }
        
        statusRetweetedView.snp_makeConstraints { (make) in
            make.top.equalTo(statusOriginalView.snp_bottom)
            make.left.right.equalTo(contentView)
            //设置测试高度
            //make.height.equalTo(60)
        }
        
        statusToolBar.snp_makeConstraints { (make) in
            //生成顶部约束 并且使用属性进行保存
            self.toolbarTopCons = make.top.equalTo(statusRetweetedView.snp_bottom).constraint

            make.right.left.equalTo(self.contentView)
            //设置高度
            make.height.equalTo(36)
        }
        
        //给cell的contentView的底部添加约束
        //snp --> 取消了视图的frame 布局
        contentView.snp_makeConstraints { (make) in
            //将contentView的约束都加上
            make.top.left.right.equalTo(self)
            make.bottom.equalTo(statusToolBar.snp_bottom)
        }
    }
    

    //懒加载子视图
    //原创微博视图
    private lazy var statusOriginalView: HJStatusOriginalView = HJStatusOriginalView()
    //懒加载底部工具条
    private lazy var statusToolBar: HJStatusToolBar = HJStatusToolBar()
    //转发微博视图
    private lazy var statusRetweetedView: HJStatusRetweetedView = HJStatusRetweetedView()


}
